package cycling.taxi.com.person;

import android.os.AsyncTask;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cycling.taxi.com.Interface.OnRefreshListener;
import cycling.taxi.com.R;
import cycling.taxi.com.adapter.MyAfterAdapter;
import cycling.taxi.com.bean.SalesInfo;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.view.RefreshListView;

/**
 * 售后服务
 */
public class AftersalesActivity extends AppCompatActivity  implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView back;
    private TextView title_text;
    private List<SalesInfo> SalesInfos;
    private RefreshListView refreshListView;
    private MyAfterAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aftersales);
        ExitApplication.getInstance().addActivity(this);
        InitView();
    }
    private void InitView() {
        refreshListView= (RefreshListView) findViewById(R.id.refresh_listview);
        //模拟数据
        SalesInfos=new ArrayList<SalesInfo>();
        for (int i=0;i<10;i++){
            SalesInfos.add(new SalesInfo("塔锡科技","桂林市七星区创新大厦","1837830755"+i));
        }
        adapter=new MyAfterAdapter(this,SalesInfos);
        back= (ImageView) findViewById(R.id.back);
        title_text= (TextView) findViewById(R.id.title_text);
        title_text.setText("售后服务");
        back.setOnClickListener(this);

        refreshListView.setAdapter(adapter);
        refreshListView.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                // 异步查询数据
                new AsyncTask<Void, Void, Void>(){

                    @Override
                    protected Void doInBackground(Void... params) {
                        SystemClock.sleep(2000);
                        SalesInfos.add(0,new SalesInfo("下拉刷新","桂林市七星区创新大厦","18378307553"));
                        return null;
                    }

                    protected void onPostExecute(Void result) {
                        adapter.notifyDataSetChanged();

                        // 隐藏头布局
                        refreshListView.onRefreshFinish();
                    }
                }.execute(new Void[]{});
            }

            @Override
            public void onLoadMoring() {
                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        SystemClock.sleep(2000);
                        SalesInfos.add(new SalesInfo("加载更多","桂林市七星区创新大厦","18378307553"));
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        super.onPostExecute(result);
                        adapter.notifyDataSetChanged();
                        refreshListView.onRefreshFinish();
                    }

                }.execute(new Void[]{});
            }
        });
        refreshListView.setOnItemClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // ToastUtil.getInstance().toast(SalesInfos.get(position).toString());
    }
}
